package org.mspring.mlog.web.module.admin.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.web.module.admin.Admin_AbstractWidget;
import org.mspring.mlog.web.support.log.Log;
import org.mspring.platform.config.ConfigService;
import org.mspring.platform.mail.MailService;
import org.mspring.platform.security.exception.UNLoginException;
import org.mspring.platform.web.freemarker.widget.stereotype.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Gao Youbo
 * @since 2013-1-10
 */
@Action
@RequestMapping("/admin/system/mail")
public class Admin_MailWidget extends Admin_AbstractWidget {

    @Autowired
    private ConfigService configService;
    @Autowired
    private MailService mailService;

    /**
     * 显示"邮件设置"
     *
     * @param configs
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/config")
    public String config(@RequestParam Map<String, String> configs, HttpServletRequest request, HttpServletResponse response, Model model) {
        configs = configService.getConfigs();
        model.addAllAttributes(configs);
        return "/admin/system/mail/config";
    }

    /**
     * 保存"邮件设置"
     *
     * @param configs
     * @param request
     * @param response
     * @param model
     * @return
     * @throws UNLoginException
     */
    @RequestMapping("/config/save")
    @Log
    public String config_save(@RequestParam Map<String, String> configs, HttpServletRequest request, HttpServletResponse response, Model model) throws UNLoginException {
        configService.setConfigs(configs);
        return "redirect:/admin/system/mail/config";
    }

    /**
     * 显示邮件测试页面
     *
     * @param configs
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/test")
    public String testView(@RequestParam Map<String, String> configs, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "/admin/system/mail/test";
    }

    /**
     * 发送测试邮件
     *
     * @param configs
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/test/send")
    @Log
    public String sendTestMail(@RequestParam Map<String, String> configs, HttpServletRequest request, HttpServletResponse response, Model model) {
        boolean success = true;
        String message = "success!";
        try {
            String mail_to = request.getParameter("mail_to");
            String mail_content = request.getParameter("mail_content");
            mailService.sendMail(mail_to, "", "M-LOG测试邮件", mail_content);
        } catch (Exception e) {
            success = false;
            message = e.getMessage();
        }
        model.addAttribute("success", success);
        model.addAttribute("message", message);
        return "/admin/system/mail/test";
    }
}
